$(document).ready(function () {
    globalExceptionExec();
    WebSocketConnect();
    initUI();
    shangZhuang();
});
function initUI() {
    $('#qun_members_table').datagrid({
        idField: 'id',
        url: baseUrl + '/qun_members.do',
        onRowContextMenu: function (e, index, row) {
            e.preventDefault();
            $(this).datagrid("clearSelections");
            $(this).datagrid("selectRow", index);
            $('#qun_members_table_menu').bind("contextmenu",function(e){
                return false;
            });
            $('#qun_members_table_menu').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
            e.preventDefault();
        }
    });
    $('#members_table').datagrid({
        idField: 'id',
        url: baseUrl + '/members.do',
        pagination: true,
        pageSize: 500,
        pageList: [100, 200, 300, 400, 500],
        onRowContextMenu: function (e, index, row) {
            e.preventDefault();
            $(this).datagrid("clearSelections");
            $(this).datagrid("selectRow", index);
            $('#members_table_menu').bind("contextmenu",function(e){
                return false;
            });
            $('#members_table_menu').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
            e.preventDefault();
        }
    });
    $('#xiazhu_table').datagrid({
        idField: 'id',
        url: baseUrl + '/pod/xiazhu.do'
    });
    $('#hongbao_table').datagrid({
        idField: 'id',
        url: baseUrl + '/pod/hongbao.do'
    });
    $('#start').linkbutton({
        onClick: function () {
            $.post(actionUrl + "start", function (data, textStatus, jqXHR) {

            }, "json");
        }
    });
    $('#tingzhixiazhu').linkbutton({
        onClick: function () {
            $.post(actionUrl + "tingzhixiazhu", function (data, textStatus, jqXHR) {

            }, "json");
        }
    });
    $('#calculate').linkbutton({
        onClick: function () {
            $.messager.confirm({
                title: '请确认',
                msg: '确认结算?',
                fn: function (r) {
                    if (r) {
                        $.post(actionUrl + "calculate", function (data, textStatus, jqXHR) {
                            $.messager.alert({title: '提示', msg:  JSON.stringify(data)});
                            $('#members_table').datagrid('reload');
                            $('#qun_members_table').datagrid('reload');
                            $('#xiazhu_table').datagrid('reload');
                            $('#hongbao_table').datagrid('reload');
                            shangZhuang();
                        }, "json");

                    }
                }
            });
        }
    });
    $('#fire').linkbutton({
        onClick: function () {
            $.post(actionUrl + "fire", function (data, textStatus, jqXHR) {

            }, "json");
        }
    });
    $('#showAccount').linkbutton({
        onClick: function () {
            $.post(actionUrl + "showAccount", function (data, textStatus, jqXHR) {
                $.messager.show({title: '提示', msg:  '消息发送成功'});
            }, "json");
        }
    });
    $('#setting_message_template').linkbutton({
        onClick: function () {
            $('#setting_message_template_dialog').dialog({
                title: 'My Dialog',
                width: '80%',
                height: '80%',
                closed: false,
                cache: false,
                href: 'app/setting_message_template.html',
                modal: true,
                buttons:[{
                    iconCls:'icon-ok',
                    text:'确定',
                    handler:function(){
                        $('#setting_message_template_form').submit();
                        $.messager.show({
                            title:'提示',
                            msg:'操作成功',
                            timeout:1000,
                            showType:'slide'
                        });
                        $('#setting_message_template_dialog').dialog('close');
                    }
                },{
                    iconCls:'icon-cancel',
                    text:'取消',
                    handler:function(){
                        $('#setting_message_template_dialog').dialog('close');
                    }
                }]
            });
        }
    });
    $('#setting_rule_code').linkbutton({
        onClick: function () {
            $('#setting_rule_code_dialog').dialog({
                title: 'My Dialog',
                width: '80%',
                height: '80%',
                closed: false,
                cache: false,
                href: 'app/setting_rule_code.html',
                modal: true,
                buttons:[{
                    iconCls:'icon-ok',
                    text:'确定',
                    handler:function(){
                        $('#setting_rule_code_dialog').dialog('close');
                    }
                },{
                    iconCls:'icon-cancel',
                    text:'取消',
                    handler:function(){
                        $('#setting_rule_code_dialog').dialog('close');
                    }
                }]
            });
        }
    });
    $('#setting_rule_config').linkbutton({
        onClick: function () {
            $('#setting_rule_config_dialog').dialog({
                title: 'My Dialog',
                width: '30%',
                height: '80%',
                closed: false,
                cache: false,
                href: 'app/setting_rule_config.html',
                modal: true,
                buttons:[{
                    iconCls:'icon-ok',
                    text:'确定',
                    handler:function(){
                        var rows = $('#setting_rule_config_propertygrid').propertygrid('getChanges');
                        $.post(baseUrl + "/caculateenv/change.do",{change: JSON.stringify(rows)}, function (data, textStatus, jqXHR) {
                            $.messager.show({
                                title:'提示',
                                msg:'操作成功',
                                timeout:1000,
                                showType:'slide'
                            });
                            $('#setting_rule_config_dialog').dialog('close');
                        }, "json");
                    }
                },{
                    iconCls:'icon-cancel',
                    text:'取消',
                    handler:function(){
                        $('#setting_rule_config_dialog').dialog('close');
                    }
                }]
            });
        }
    });

    $('[qun_shangfen]').click(function () {
        shangfen('#qun_members_table');
    });
    $('[global_shangfen]').click(function () {
        shangfen('#members_table');
    });
}

function shangfen(grid){
    var row = $(grid).datagrid('getSelected');
    if(row){
        $('#qun_shangfen_dialog').dialog({
            title: '玩家上分',
            width: '220',
            height: '150',
            closed: false,
            cache: false,
            href: 'app/shangfen.html',
            modal: true,
            buttons:[{
                iconCls:'icon-ok',
                text:'确定',
                handler:function(){
                    var row = $(grid).datagrid('getSelected');
                    var value = $('#text-input-shangfen').val();
                    if(!value){
                        $.messager.show({title: '警告', msg:  '请输入要上多少分'});
                        return;
                    }
                    $.post(baseUrl + "/members/shangfen.do",{id: row.id, value: value}, function (data, textStatus, jqXHR) {
                        $('#members_table').datagrid('reload');
                        $('#qun_members_table').datagrid('reload');
                    }, "json");
                    $('#qun_shangfen_dialog').dialog('close');
                }
            },{
                iconCls:'icon-cancel',
                text:'取消',
                handler:function(){
                    $('#qun_shangfen_dialog').dialog('close');
                }
            }]
        });
    }else{
        $.messager.show({title: '提示', msg:  '请选中要上分的玩家'});
    }
}

function shangZhuang() {
    $.get(currShangZhuangUrl, function (data, textStatus, jqXHR) {
        $('#zhuang_name').html(data.zhuangName);
        $('#zhuang_money').html(data.zhuangMoney);
        if(data.zhuangName != null){
            $.messager.show({title: '提示', msg: data.zhuangName  + ' 已上庄'});
        }
    }, "json");
}

function xiaZhu(value) {
    var xiazhu = JSON.parse(value);
    var index = $('#xiazhu_table').datagrid('getRowIndex', xiazhu.msg.id);
    if(index != -1){
        $('#xiazhu_table').datagrid('updateRow', {index: index, row: {xia_zhu : xiazhu.msg.xia_zhu}});
    }else{
        $('#xiazhu_table').datagrid('appendRow', xiazhu.msg);
    }
}

function cheXiao(value) {
    var index = $('#xiazhu_table').datagrid('getRowIndex', JSON.parse(value));
    $('#xiazhu_table').datagrid('deleteRow', index);
}

function qiangHongBao(value) {
    $('#hongbao_table').datagrid('reload');
}

function qun_members_table_tb_search(value) {
    $('#qun_members_table').datagrid('load', {name: value});
}

function members_table_tb_search(value) {
    $('#members_table').datagrid('load', {name: value});
}

function WebSocketConnect() {
    if ("WebSocket" in window) {
        var ws = new WebSocket(webSocketUrl);
        ws.onopen = function () {
            $.messager.show({title: '系统提示', msg:  "服务器已连接"});
        };

        ws.onmessage = function (evt) {
            var received_msg = evt.data;
            var obj = JSON.parse(received_msg);
            if (obj.type == 0) {

            } else if (obj.type == 1) {
                shangZhuang();
            } else if (obj.type == 2) {
                xiaZhu(received_msg);
            } else if (obj.type == 3) {
                qiangHongBao(received_msg);
            } else if (obj.type == 4) {
                cheXiao(received_msg);
            } else if (obj.type == 5) {
                $('#members_table').datagrid('reload');
                $('#qun_members_table').datagrid('reload');
                $('#xiazhu_table').datagrid('reload');
                $('#hongbao_table').datagrid('reload');
                shangZhuang();
            } else if (obj.type == 6) {
                $('#members_table').datagrid('reload');
                $('#qun_members_table').datagrid('reload');
                $('#xiazhu_table').datagrid('reload');
                $('#hongbao_table').datagrid('reload');
                shangZhuang();
            }else if (obj.type == 7) {
                $.messager.show({title: '提示', msg: obj.msg.msg});
            }
        };

        ws.onclose = function () {
            $.messager.show({title: '系统异常', msg:  "与服务器断开连接"});
        };
    } else {

    }
}

function globalExceptionExec() {
    $.ajaxSetup({
        error: function (jqXHR, textStatus, errorThrown) {
            switch (jqXHR.status) {
                case(500):
                    $.messager.show({title: '系统异常', msg:  jqXHR.responseText});
                    break;
                case(400):
                    $.messager.show({title: '数据不合法', msg:  JSON.parse(jqXHR.responseText).msg});
                    break;
                case(401):
                    alert("未登录");
                    break;
                case(403):
                    alert("无权限执行此操作");
                    break;
                case(408):
                    alert("请求超时");
                    break;
                default:
                    alert("找不到服务器");
            }
        }
    });
}